import 'dart:convert';

import 'package:flutter_deer/util/input_formatter/mny_format.dart';

class SpeakStyleData {
  int speakId;
  String speakStyle;
  String speakContent;
  String speakSupply;

  SpeakStyleData({
    this.speakId = 0,
    this.speakStyle = '',
    String? speakContent,
    String? speakSupply,
  })  : this.speakContent = speakContent ?? SpeakEnu.getDefaultSpeakDemo(speakId),
        this.speakSupply = speakSupply ?? _getDefaultSupply(speakId);

  static String _getDefaultSupply(int speakId) {
    if (speakId == SpeakTypeEnu.SPEAK_COUPON.speakId) {
      return '成功';
    } else {
      return '交易成功';
    }
  }

  // toJson
  Map<String, dynamic> toJson() {
    return {
      'speakId': speakId,
      'speakStyle': speakStyle,
      'speakContent': speakContent,
      'speakSupply': speakSupply,
    };
  }

  // 解析组装播报内容
  String replaceSpeakContent(Map<String, dynamic> content,{bool advanceOrder = false}) {
    final buffer = StringBuffer();
    final member = content['memberInfo'] as Map<String, dynamic>;
    final orderInfo = content['orderInfo'] as Map<String, dynamic>;
    final memberInfo = content['memberInfo'] as Map<String, dynamic>;
    final paymentInfo = content['paymentInfo'] as Map<String, dynamic>;
    final consumDetails = content['consumDetails'] as Map<String, dynamic>;

    final phone = '${member['phone']}';
    final phone4 = phone.phone4.isEmpty ? '' : '尾号${phone.phone4}';
    final payTime = '${orderInfo['payTime']}';
    final shelfNo = '${consumDetails['shelfNo']}';
    final shelfStr = shelfNo.isEmpty ? '' : '${shelfNo}号枪';
    final gasNO = '${consumDetails['gasNO']}';
    final oilNO = '${consumDetails['oilNO']}'.replaceAll('#', '号');
    final oilType = gasNO + oilNO;
    final count = '${consumDetails['count']}'.replaceAll('L', '升').replaceAll('T', '吨');
    final discountAmountTotal = '${paymentInfo['discountAmountTotal']}'.replaceAll('.00', '').replaceAll('null', '零');;
    final payTypeName = '${content['payChannelName']}';
    final clientType = '${content['clientType']}';
    String deviceType = '';
    if (clientType == 'SaaSxcx') {
      if (advanceOrder) {
        deviceType = '预付下单';
      } else {
        deviceType = '小程序下单';
      }
    } else if (clientType == 'SaaSpos') {
      deviceType = '剖丝机付款';
    } else if (clientType == 'SaaSzzj') {
      deviceType = '自助机付款';
    } else if (clientType == 'SaaSyjdp') {
      deviceType = '油机大屏付款';
    } else if (clientType == 'SquarePayApi') {
      deviceType = '流量平台付款';
    } else if (clientType == 'SaaSspj') {
      deviceType = '双屏机付款';
    }

    final rechargeArrivedAmount = '${paymentInfo['receivedAmount']}'.replaceAll('.00', '').replaceAll('null', '零');
    final payAmount = '${paymentInfo['payableAmount']}'.replaceAll('.00', '').replaceAll('null', '零');
    final rechargeGiftAmount = '${paymentInfo['giveRefundAmount']}'.replaceAll('.00', '').replaceAll('null', '零');
    final accountTypeName = '${memberInfo['cardType']}';
    final isAbnormalOrder = content['isAbnormal'] == 1 ? '异常订单' : '';

    buffer.write(isAbnormalOrder);
    buffer.write(speakContent);
    buffer.write(speakSupply);
    final result = buffer.toString()
          .replaceAll(SpeakEnu.STYLE_PHONE, '$phone4 ')
          .replaceAll(SpeakEnu.STYLE_PAY_TIME, '$payTime ')
          .replaceAll(SpeakEnu.STYLE_GUN_NZL, '$shelfStr ')
          .replaceAll(SpeakEnu.STYLE_OIL_TYPE, '$oilType ')
          .replaceAll(SpeakEnu.STYLE_PAY_SCENE, '$deviceType ')
          .replaceAll(SpeakEnu.STYLE_PAY_TYPE, '$payTypeName ')
          .replaceAll(SpeakEnu.STYLE_PAY_MONEY, '收款${payAmount}元 ')
          .replaceAll(SpeakEnu.STYLE_BUY_NUMBER, '$count ')
          .replaceAll(SpeakEnu.STYLE_PAY_DISCOUNT, '优惠${discountAmountTotal}元 ')
          .replaceAll(SpeakEnu.STYLE_RECHARGE, '充值${payAmount}元 ')
          .replaceAll(SpeakEnu.STYLE_GIT_RECHARGE, '赠送${rechargeGiftAmount}元 ')
          .replaceAll(SpeakEnu.STYLE_RECEIVED, '到账${rechargeArrivedAmount} ')
          .replaceAll(SpeakEnu.STYLE_CARD_TYPE, '$accountTypeName ')
          .replaceAll('[', '')
          .replaceAll(']', '');
    return result;
  }
}

enum SpeakTypeEnu {
  SPEAK_OIL('能源订单', 0),
  SPEAK_RECHARGE('储值', 1),
  SPEAK_STORE('便利店', 2),
  SPEAK_TONG('通付', 3),
  SPEAK_COUPON('核销', 4);

  final String speakStyle;
  final int speakId;

  const SpeakTypeEnu(this.speakStyle, this.speakId);

  static String? format(int orderStatus) {
    for (final value in SpeakTypeEnu.values) {
      if (value.speakId == orderStatus) {
        return value.speakStyle;
      }
    }
    return null;
  }

  // 获取id
  static int getSpeakId(String speakStyle) {
    for (final value in SpeakTypeEnu.values) {
      if (value.speakStyle == speakStyle) {
        return value.speakId;
      }
    }
    return 0;
  }

  static List<CommonData> getList(int speakId) {
    // 使用 if-else 替代 switch，这样可以与枚举强关联
    if (speakId == SpeakTypeEnu.SPEAK_OIL.speakId) {
      return [
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PHONE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TIME),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_GUN_NZL),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_OIL_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_SCENE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_MONEY),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_DISCOUNT),
      ];
    } else if (speakId == SpeakTypeEnu.SPEAK_RECHARGE.speakId) {
      return [
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PHONE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TIME),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_CARD_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_RECEIVED),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_RECHARGE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_GIT_RECHARGE),
      ];
    } else if (speakId == SpeakTypeEnu.SPEAK_STORE.speakId) {
      return [
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PHONE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TIME),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_BUY_NUMBER),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_SCENE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_MONEY),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_DISCOUNT),
      ];
    } else if (speakId == SpeakTypeEnu.SPEAK_COUPON.speakId) {
      return [
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PHONE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_COUPON_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_COUPON_NAME),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_INTEGRAL_AMOUNT),
      ];
    } else if (speakId == SpeakTypeEnu.SPEAK_TONG.speakId) {
      return [
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_TONG_FU),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_MONEY),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TYPE),
      ];
    } else {
      return [
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TIME),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_GUN_NZL),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_OIL_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_SCENE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_TYPE),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_MONEY),
        CommonData(canUse: true, payTypeName: SpeakEnu.STYLE_PAY_DISCOUNT),
      ];
    }
  }
}

class SpeakEnu {
  static const String STYLE_PHONE = '手机尾号';
  static const String STYLE_PAY_TIME = '支付时间';
  static const String STYLE_GUN_NZL = '加注枪号';
  static const String STYLE_OIL_TYPE = '能源商品';
  static const String STYLE_PAY_SCENE = '消费场景';
  static const String STYLE_PAY_TYPE = '支付方式';
  static const String STYLE_PAY_MONEY = '消费金额';
  static const String STYLE_BUY_NUMBER = '商品数';
  static const String STYLE_PAY_DISCOUNT = '优惠';
  static const String STYLE_SUPPLY_STR = '补充播报';
  static const String STYLE_RECHARGE = '充值金额';
  static const String STYLE_GIT_RECHARGE = '赠送金额';
  static const String STYLE_RECEIVED = '实际到账金额';
  static const String STYLE_CARD_TYPE = '卡类型';
  static const String STYLE_COUPON_TYPE = '核销方式';
  static const String STYLE_COUPON_NAME = '核销内容';
  static const String STYLE_INTEGRAL_AMOUNT = '核销积分';
  static const String STYLE_TONG_FU = '通付'; //固定文案

  static String demoFormatSpeak(String content) {
    String result = content;
    result = result.replaceAll('[$STYLE_PHONE]', '尾号9190 ');
    result = result.replaceAll('[$STYLE_PAY_TIME]', '2015年12月15日 12:57:24 ');
    result = result.replaceAll('[$STYLE_GUN_NZL]', '9号枪 ');
    result = result.replaceAll('[$STYLE_CARD_TYPE]', '个人卡 ');
    result = result.replaceAll('[$STYLE_OIL_TYPE]', '98号汽油 ');
    result = result.replaceAll('[$STYLE_RECEIVED]', '到账220元 ');
    result = result.replaceAll('[$STYLE_RECHARGE]', '充值200元 ');
    result = result.replaceAll('[$STYLE_GIT_RECHARGE]', '赠送20元 ');
    result = result.replaceAll('[$STYLE_PAY_SCENE]', '快捷下单 '); //设备付款
    result = result.replaceAll('[$STYLE_PAY_TYPE]', '小程序支付 ');
    result = result.replaceAll('[$STYLE_PAY_MONEY]', '200元 ');
    result = result.replaceAll('[$STYLE_BUY_NUMBER]', '8件商品 ');
    result = result.replaceAll('[$STYLE_PAY_DISCOUNT]', '优惠2元 ');
    result = result.replaceAll('[$STYLE_SUPPLY_STR]', '交易成功 ');
    result = result.replaceAll('[$STYLE_COUPON_TYPE]', '扫码核销 ');
    result = result.replaceAll('[$STYLE_COUPON_NAME]', '洗车券 ');
    result = result.replaceAll('[$STYLE_INTEGRAL_AMOUNT]', '消耗200积分 ');
    result = result.replaceAll('[$STYLE_TONG_FU]', '通付 ');
    return result;
  }

  static String getDefaultSpeakDemo(int id) {
    // 同样使用 if-else 替代 switch
    if (id == SpeakTypeEnu.SPEAK_OIL.speakId) {
      return '[$STYLE_GUN_NZL][$STYLE_PAY_SCENE][$STYLE_PAY_MONEY]';
    } else if (id == SpeakTypeEnu.SPEAK_RECHARGE.speakId) {
      return '[$STYLE_CARD_TYPE][$STYLE_RECHARGE]';
    } else if (id == SpeakTypeEnu.SPEAK_STORE.speakId) {
      return '[$STYLE_BUY_NUMBER][$STYLE_PAY_MONEY]';
    } else if (id == SpeakTypeEnu.SPEAK_TONG.speakId) {
      return '[$STYLE_TONG_FU][$STYLE_PAY_MONEY][$STYLE_PAY_TYPE]';
    } else if (id == SpeakTypeEnu.SPEAK_COUPON.speakId) {
      return '[$STYLE_PHONE][$STYLE_COUPON_TYPE][$STYLE_COUPON_NAME][$STYLE_INTEGRAL_AMOUNT]';
    } else {
      return '[$STYLE_GUN_NZL][$STYLE_PAY_SCENE][$STYLE_PAY_MONEY]';
    }
  }
}

class CommonData {
  String payTypeName;
  bool canUse;

  CommonData({this.payTypeName = '', this.canUse = false});
}

class CouponPushBean {
  String phone;
  String useTypeDesc; //核销方式
  String couponName; //券名称
  String sceneName; //券名称
  String consumeIntegral; //积分

  CouponPushBean({
    required this.phone,
    required this.useTypeDesc,
    required this.couponName,
    required this.sceneName,
    required this.consumeIntegral,
  });

  String getCouponShowName() {
    if (couponName.isNotEmpty) {
      return couponName;
    }
    if (sceneName.isNotEmpty) {
      return sceneName;
    }
    return '';
  }

  String getIntegral() {
    if (consumeIntegral.isNotEmpty) {
      return '消耗' + consumeIntegral + '积分';
    }
    return '';
  }
}
